package com.infore.operation.service.impl;

import com.github.pagehelper.PageHelper;
import com.infore.operation.entity.*;
import com.infore.operation.mapper.StationMapper;
import com.infore.operation.mapper.UserMapper;
import com.infore.operation.service.StationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;

/**
 * @author:wk
 * @date:Created in 16:15 2018\4\16 0016
 */
@Service
public class StationServiceImpl implements StationService {

    @Autowired
    private StationMapper stationMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Station> queryStationByUserId(String userId) {
        return stationMapper.queryStationByUserId(userId);
    }

    @Override
    public List<Equip> queryEquipBySId(String stationId) {
        List<Equip> list = stationMapper.queryEquipBySId(stationId);
        return list;
    }

    @Override
    public List<PositionStation> queryAllStationByuserId(String userId, Integer pageNum, String keyword) {
        UserInfoId infoId = userMapper.queryUserInfoId(userId);
        PageHelper.startPage(pageNum+1,20);
        List<PositionStation> list = stationMapper.queryAllStationByuserId(userId,infoId.getOpId(),infoId.getBranchId(),infoId.getPostId(),infoId.getRoleId(),keyword);
        return list;
    }

    @Override
    public List<PositionStation> queryNearStation(String userId, String lat, String lnt) {
        UserInfoId infoId = userMapper.queryUserInfoId(userId);
        Double latitude = Double.parseDouble(lat);
        Double longitude = Double.parseDouble(lnt);
        Double degree = (24901 * 1609) / 360.0;
        double raidusMile = 50000;
        Double dpmLat = 1 / degree;
        Double radiusLat = dpmLat * raidusMile;
        Double minLat = latitude - radiusLat;
        Double maxLat = latitude + radiusLat;

        Double mpdLng = degree * Math.cos(latitude * (Math.PI / 180));
        Double dpmLng = 1 / mpdLng;
        Double radiusLng = dpmLng * raidusMile;
        Double minLng = longitude - radiusLng;
        Double maxLng = longitude + radiusLng;
        List<PositionStation> list = stationMapper.queryNearStation(infoId.getPostId(),userId,infoId.getOpId(),infoId.getBranchId(),minLat.toString(),maxLat.toString(),minLng.toString(),maxLng.toString(),lnt,lat);
        return list;
    }
}
